package com.example.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.Util.HttpUtil;
import com.example.Util.Md5Util;
import com.example.Util.Tools;
import com.example.pojo.accountProperties;
import com.example.service.ApplyPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class ApplyPayServiceImpl implements ApplyPayService {

    @Autowired
    private accountProperties AccountProperties;

    @Autowired
    private com.example.pojo.UrlMethodProperties UrlMethodProperties;


    /**
     * 前端数据示例：
     {"subject":"大米饭","meta_option":"","notify_url":"https://demo.heemoney.com/Test/Api",

     "isv_app_id":"4945892105419","isv_mch_uid":"hyp2405074945890003693051E749686",

     "out_trade_no":"20240515143448","pay_option":"","total_fee":"1",
     "client_ip":"192.168.1.100","attach":"","channel_type":"BANK_CYBER",

     "app_id":"hyp17041449458900000057698DAAB00","mch_uid":"4945892094734"}
     */


    @Override
    public String ApplyPay(JSONObject reqJson) {


        /**
         *  公共请求参数
         */

        //具体业务接口名称
        String methed = UrlMethodProperties.getApplyPaymethod();
        //服务商商户号
        String isv_mch_uid = reqJson.getString("isv_mch_uid");
        //服务商的应用id
        String isv_app_id = reqJson.getString("isv_app_id");
        //商户统一编号
        String mch_uid = reqJson.getString("mch_uid");
        //应用ID，商户的应用id
        String app_id = reqJson.getString("app_id");
        //密钥md5
        String key = AccountProperties.getKey();
        //版本号,默认1.0
        String version = "1.0";
        //编码格式默认为UTF-8
        String charset = "UTF-8";
        //	商户生成签名字符串所使用的签名算法类型,目前固定MD5
        String sign_type = "MD5";
        //发送请求的时间 yyyyMMddHHmm ss
        String timestamp = Tools.GetTimeStamp14Time();


        //移除业务参数中的公共参数
        reqJson.remove("isv_mch_uid");
        reqJson.remove("isv_app_id");
        reqJson.remove("mch_uid");
        reqJson.remove("app_id");



        //组装签名串得出签名结果
        Map<String, String> map = new HashMap<>();
        map.put("method", methed);
        map.put("version", version);
        map.put("app_id", app_id);
        map.put("mch_uid", mch_uid);
        map.put("isv_mch_uid", isv_mch_uid);
        map.put("isv_app_id", isv_app_id);
        map.put("charset", charset);
        map.put("sign_type", sign_type);
        map.put("timestamp", timestamp);
        map.put("biz_content", String.valueOf(reqJson));

        String a = Tools.mapDataSortAToZ(map);
        String sign = Md5Util.getMD5(a + "&key=" + key);


        log.info("签名串：--------------------------------->>>{}", a + "&key=" + key);

        //组装最终请求参数

        Map<String, String> reqMap = new HashMap<>();
        reqMap.put("method", methed);
        reqMap.put("version", version);
        reqMap.put("app_id", app_id);
        reqMap.put("mch_uid", mch_uid);
        reqMap.put("isv_mch_uid", isv_mch_uid);
        reqMap.put("isv_app_id", isv_app_id);
        reqMap.put("charset", charset);
        reqMap.put("sign_type", sign_type);
        reqMap.put("timestamp", timestamp);
        reqMap.put("biz_content", String.valueOf(reqJson));
        reqMap.put("sign", sign);

        log.info("请求参数：--------------------------------->>>{}", JSON.toJSONString(reqMap));

        //请求地址
        String url = UrlMethodProperties.getApplyPay();

        String result = HttpUtil.sendPostJson(url, reqMap);

        log.info("返回结果：--------------------------------->>>{}", result);

        return result;
    }
}
